package net.hotelOrderSystem.service;

import net.hotelOrderSystem.domain.User;
import net.hotelOrderSystem.domain.UserExample;
import net.hotelOrderSystem.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findUser() {
        return userMapper.selectByExample(new UserExample());
    }

    @Override
    public void saveUser(User user) {
        if(userMapper.selectByPrimaryKey(user.getAccount()) == null) {
            userMapper.insert(user);
        } else {
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public void deleteUserByAccount(String account) {
        UserExample user = new UserExample();
        user.createCriteria().andAccountEqualTo(account);
        userMapper.deleteByExample(user);
    }

    @Override
    public void updateUserByAccount(String account,User user) {
        UserExample user1 = new UserExample();
        user1.createCriteria().andAccountEqualTo(account);
        userMapper.updateByExample(user,user1);
    }

    @Override
    public List<User> findUserByPremission(String premission) {
        UserExample user = new UserExample();
        user.createCriteria().andPremissionLessThan(premission);
        return userMapper.selectByExample(user);
    }


    public User findUserByAccount(String account) {
        UserExample user = new UserExample();
        user.createCriteria().andAccountEqualTo(account);
        if(userMapper.selectByExample(user).isEmpty())
            return null;
        else
             return userMapper.selectByExample(user).get(0);
    }

}
